package io.github.cocoa.module.socket.listener;

import com.corundumstudio.socketio.SocketIOServer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 应用程序已关闭事件侦听器
 */
@Slf4j
@Component
public class ApplicationClosedEventListener implements ApplicationListener<ContextClosedEvent> {

    @Resource
    @Qualifier(value = "socketIOServer")
    private SocketIOServer socketIOServer;

    @Override
    public void onApplicationEvent(ContextClosedEvent event) {
        log.info("## ContextClosedEvent; @event: {}", event);
        if (socketIOServer != null) {
            log.info("## socketIOServer @stop");
            socketIOServer.stop();
        }
    }
}
